<?php

class Welcome extends Controller {

	function Welcome(){
		parent::Controller();	
		$this->load->model("M_Common","common");
	}
	
	function index(){
		$data["title"] = "Login";
		$this->load->view('index',$data);
	}
	function home(){
		$data["title"] 			= 'Pagina principala';
		$data["session"] 		= $this->session->userdata('us_');	
		
		$company 	= $this->common->mSelectCompany($data['session']['us_C']);
	    $data['compania'] = $company[0]['nume'];

		if ($this->myxml->load('http://www.bnro.ro/nbrfxrates.xml')) { 
			$array = $this->myxml->parse();
			$curs 	= array();
			$names 	= array("AUD","BGN","CAD","CHF","CZK","DKK","EGP","EUR","GBP","HUF","JPY","MDL","NOK","PLN","RUB","SEK","SKK","TRY","USD","XAU","XDR");
			$values = $array['DataSet'][0]['Body'][0]['Cube'][0]['Rate'];
			
			$curs['data'] 	= DateConvertFromMysql($array['DataSet'][0]['Header'][0]['SendingDate'][0],'d-m-Y');

			$i = 0;
			while($i < count($names)) {
				$curs['values'][$names[$i]] = $values[$i];
				$i++;
			}					
			$data['curs'] = $curs;
		} 

		$rowFacturi 		= $this->common->mSelectCount('facturi');	
		$rowFacturiAnulate 	= $this->common->mSelectCount('facturi_anulate');
		$rowClienti 		= $this->common->mSelectHPClienti('');
		$rowClientiPF 		= $this->common->mSelectHPClienti('3');	
		$rowClientiPJ 		= $this->common->mSelectHPClienti('4');		
		
		$data['facturi'] 			= $rowFacturi->value;	
		$data['facturi_anulate'] 	= $rowFacturiAnulate->value;
		$data['clienti'] 			= $rowClienti->value;
		$data['clienti_pf'] 		= $rowClientiPF->value;
		$data['clienti_pj'] 		= $rowClientiPJ->value;

		$this->load->view('home',$data);
	}	
	function main(){
		$data["title"] 			= 'Pagina principala';
		$data["session"] 		= $this->session->userdata('us_');
		
		if ($this->myxml->load('http://www.bnro.ro/nbrfxrates.xml')) { 
			$array = $this->myxml->parse();
			$curs 	= array();
			$names 	= array("AUD","BGN","CAD","CHF","CZK","DKK","EGP","EUR","GBP","HUF","JPY","MDL","NOK","PLN","RUB","SEK","SKK","TRY","USD","XAU","XDR");
			$values = $array['DataSet'][0]['Body'][0]['Cube'][0]['Rate'];
			
			$curs['data'] 	= DateConvertFromMysql($array['DataSet'][0]['Header'][0]['SendingDate'][0],'d-m-Y');

			$i = 0;
			while($i < count($names)) {
				$curs['values'][$names[$i]] = $values[$i];
				$i++;
			}					
			$data['curs'] = $curs;
		} 	
		
		$rowFacturi 		= $this->common->mSelectCount('facturi');	
		$rowFacturiAnulate 	= $this->common->mSelectCount('facturi_anulate');
		$rowClienti 		= $this->common->mSelectHPClienti('');
		$rowClientiPF 		= $this->common->mSelectHPClienti('3');	
		$rowClientiPJ 		= $this->common->mSelectHPClienti('4');		
		
		$data['facturi'] 			= $rowFacturi->value;	
		$data['facturi_anulate'] 	= $rowFacturiAnulate->value;
		$data['clienti'] 			= $rowClienti->value;
		$data['clienti_pf'] 		= $rowClientiPF->value;
		$data['clienti_pj'] 		= $rowClientiPJ->value;
		
		$this->load->view('common/index',$data);
	}		
	function tree(){
		$nodes = $this->common->mDisplayChildren($this->input->post("node"));
		echo json_encode($nodes);
	}	
	function login(){
	    $sLogin = $this->input->post("user");
	    $sPassw = $this->input->post("pass");
	    if($this->auth->authenticate($sLogin,$sPassw)){
			echo json_encode(array(
			        'success'=> true,
			        'code' => 1
			    ));  		
	    }else{
			echo json_encode(array(
			        'success'=> true,
			        'code' => 0
			    ));  
	    }
	}
	function logout(){
	    $this->session->unset_userdata('us_');
		$this->session->sess_destroy();
		redirect("welcome","location");
	}	
}
?>